package ldkxingzhe.l365calendar.fragment;

import android.support.v4.app.Fragment;
import android.util.Log;

import cn.pedant.SweetAlert.SweetAlertDialog;
import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.activity.AllEditActivity;

/**
 * 所有可编辑的Fragment的基类
 * Created by ke on 16-1-24.
 */
public class AllEditFragmentBase extends Fragment{
    @SuppressWarnings("unused")
    private final static String TAG = "AllEditFragmentBase";

    protected boolean isChanged = false;

    /**
     * 点击取消时
     * @return  true -- 表示消耗此次动作
     */
    public boolean onCancelClick(){
        if(!isChanged){
            Log.v(TAG, "isChanged is false, and return");
            return false;
        }
        new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
                .setTitleText(getString(R.string.waring))
                .setContentText(getString(R.string.quit_lost_edt))
                .setConfirmText(getString(R.string.confirm))
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        Log.v(TAG, "onConfimClick");
                        sweetAlertDialog.dismiss();
                        getActivity().finish();
                    }
                })
                .setCancelText(getString(R.string.cancel))
                .show();
        return true;
    }

    /**
     * 点击保存时
     */
    public void onSaveClick(){
        Log.v(TAG, "onSaveClick");
    }


    public void showAlert(int stringId) {
        new SweetAlertDialog(getActivity(), SweetAlertDialog.ERROR_TYPE)
                .setTitleText(getString(R.string.waring))
                .setContentText(getString(stringId))
                .setConfirmText(getString(R.string.i_know))
                .show();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        setIsChanged(isChanged);
        Log.v(TAG, "setUserVisibleHit");
    }

    public void setIsChanged(boolean isChanged) {
        this.isChanged = isChanged;
        if(getActivity() instanceof AllEditActivity){
            ((AllEditActivity)getActivity()).setSaveEnable(isChanged);
        }
    }
}
